//
//  CustomTabBar.swift
//  WeiBo
//
//  Created by 江潘 on 15/6/24.
//  Copyright © 2015年 江潘. All rights reserved.
//

import UIKit

// 自定义tabBar
class CustomTabBar: UITabBar {
    
    let btnCount = 5;
    
    override init(frame: CGRect) {

        super.init(frame: frame)
        
        tintColor = UIColor.orangeColor()
        
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
   // UITabBarButton 是私有接口，代码中不要出现（使用可能会被拒绝上架）
        
        let w = bounds.width / CGFloat(btnCount)
        
        let rect = CGRectMake(0, 0, w, bounds.height)
        
        var index = 0
        
        for v in subviews {
                    
            if v is UIControl && !(v is UIButton) {
         
            
                v.frame = CGRectOffset(rect, w * CGFloat(index), 0)
                
                index += index == 1 ? 2 : 1
            }
          
            btn.frame = CGRectOffset(rect, w * 2, 0)
        }
    }
  
  // 懒加载 button
    lazy var btn: UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(btn)
        
        return btn

        }()
}
